package myThread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Demo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // method1();

        // method2();

        // method3();

        // method4();

        // method5();


    }

    private static void method5() throws InterruptedException {
        // Java中阻塞队列由BlockingQueue类实现
        // 底层原理：ArrayBlockingQueue底层为数组，有界
        // LinkedBlockingQueue 底层为链表，无界，实际上不是真正的无界，上界为int最大整型数

        // 参数为阻塞队列的容量
        ArrayBlockingQueue<String> a = new ArrayBlockingQueue<>(1);

        // put方法为生产者往阻塞队列中放入资源，当队列满时等待。
        a.put("apple");

        // take方法为消费者从队列中拿资源，当队列空时等待。
        System.out.println(a.take());
    }

    private static void method4() {
        Desk desk = new Desk();

        // 生产者消费者问题
        Foodie f = new Foodie(desk);
        Cooker c = new Cooker(desk);

        f.start();
        c.start();
    }

    private static void method3() {
        MyThread m1 = new MyThread("线程1");
        MyRunnable mr = new MyRunnable();
        Thread m2 = new Thread(mr);

        m1.start();
        m2.start();

        m1.setPriority(10);
        System.out.println(m1.getPriority());
        System.out.println(m2.getPriority());
    }

    private static void method2() {
        Thread t = Thread.currentThread();// 获取当前正在运行的线程
        String name = t.getName();// 获取主线程的名称
        System.out.println(name);
    }

    private static void method1() throws InterruptedException, ExecutionException {
        // 线程开启后执行里面的call方法
        MyCallable m1 = new MyCallable();

        // 可也获取线程开启后返回的结果，也可以作为参数传递给Thread对象
        FutureTask<String> f1 = new FutureTask<>(m1);

        Thread t1 = new Thread(f1);
        t1.start();

        System.out.println(f1.get()); // get方法获取线程执行后的结果
    }
}
